package com.common.baselibrary.utils;

import android.os.Handler;
import android.os.HandlerThread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RunnableFuture;

/**
 * Created by xiangyang550 on 15/8/6.
 */
public class ThreadPoolUtil {

    private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(3);

    private static HandlerThread mHandlerThread = new HandlerThread("ThreadPoolUtilHandlerThread");

    private static Handler mThreadHandler = null;


    /**
     * Execute.
     *
     * @param runnable the runnable
     */
    public  static void execute(Runnable runnable){
        THREAD_POOL.execute(runnable);
    }

    /**
     * 在Looper初始化后的线程中执行
     * @param runnable
     */
    public static void executeWithLooper(Runnable runnable) {
        if (mThreadHandler == null) {
            mHandlerThread.start();
            mThreadHandler = new Handler(mHandlerThread.getLooper());
        }
        mThreadHandler.post(runnable);
    }


}
